package MCQReaderInterface;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.TitledBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import MCQReaderModel.PageNode;

public class MCQReaderTree extends JPanel {

	private static final long serialVersionUID = 1L;
	JTree mainTree;
	DefaultMutableTreeNode racine;
	DefaultMutableTreeNode selected;

	public MCQReaderTree() {
		super(new BorderLayout());
		setVisible(true);
		buildTree();
		TitledBorder titled = new TitledBorder("Selected zone");
		titled
				.setBorder(BorderFactory
						.createLineBorder(new Color(100, 155, 0)));
		setBorder(titled);
		setPreferredSize(getSize());
		selected = null;

		mainTree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) mainTree
						.getLastSelectedPathComponent();
				if (node == null)
					return;
				selected = node;
				MainInterface.thisIsIt.getRightFrame().getPdfSelections()
						.repaint();
			}

		});

	}

	private void buildTree() {
		racine = new DefaultMutableTreeNode("Zones délimitées");
		mainTree = new JTree(racine);
		this.add(new JScrollPane(mainTree));
	}

	public void openPDF(int pagesNumber) {
		racine.removeAllChildren();
		for (int i = 1; i <= pagesNumber; i++)
			racine.add(new PageNode(i));
		mainTree.updateUI();
		mainTree.treeDidChange();
		repaint();
	}

	static void parcourirNoeud(Object r) {
		TreeNode root = (TreeNode) r;
		System.out.println(root);
		for (int i = 0; i < root.getChildCount(); i++) {
			if (root.getChildAt(i).isLeaf())
				System.out.println(root.getChildAt(i));
			else
				parcourirNoeud(root.getChildAt(i));
		}
	}

	PageNode currentNodePage() {
		int currentPage = MainInterface.thisIsIt.getRightFrame()
				.getPdfDisplay().getCurrentPageNum();
		for (int i = 0; i < racine.getChildCount(); i++) {
			if (((PageNode) racine.getChildAt(i)).getNumPage() == currentPage)
				return (PageNode) racine.getChildAt(i);
		}
		return null;
	}

	public JTree getMainTree() {
		return mainTree;
	}

	public DefaultMutableTreeNode getRacine() {
		return racine;
	}

	public void setRacine(DefaultMutableTreeNode racine) {
		this.racine = racine;
	}

	public void setMainTree(JTree mainTree) {
		this.mainTree = mainTree;
	}

	/*public void save(String filePath) {
		try {
			FileOutputStream fos = new FileOutputStream(filePath);
			XMLEncoder o = new XMLEncoder(new BufferedOutputStream(fos));
			o.writeObject(mainTree.getModel());
			o.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void load(String filePath) {
		try {
			XMLDecoder d = new XMLDecoder(new BufferedInputStream(
					new FileInputStream(filePath)));
			mainTree.setModel((TreeModel) d.readObject());
			mainTree.treeDidChange();
			d.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
		setRacine((DefaultMutableTreeNode) mainTree.getModel().getRoot());
	}*/
}
